package cn.lg.soar.general.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.general.api.constant.ModuleConst;
import cn.lg.soar.general.entity.WebPosition;
import cn.lg.soar.general.model.WebContentVo;
import cn.lg.soar.general.model.WebPositionVO;
import cn.lg.soar.general.service.IWebPositionService;
import cn.lg.soar.mvc.annotations.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * 页面位置
 * @author luguoxiang
 * @since 2023-03-19
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Api(tags = "页面位置")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/web-position")
public class WebPositionController extends BaseController<WebPosition, IWebPositionService> {

    @Override
    protected Serializable getId(WebPosition entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "页面位置";

    @Log(value = "新增" + SUBJECT, data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody @Validated WebPositionVO data) {
        return HttpResult.data(service.create(data));
    }

    @Log(value = "修改" + SUBJECT, data = "#p0", dataId = "#p0.id")
    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Validated WebPositionVO data) {
        service.update(data);
        return HttpResult.success();
    }

    @Log(value = "删除" + SUBJECT, data = "#p0", type = Log.Type.TEXT)
    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        service.remove(ids);
        return HttpResult.success();
    }

    @DetailMapping
    public HttpResult<WebPositionVO> detail(@Id Long id) {
        return HttpResult.data(service.detail(id));
    }

    @QueryMapping
    public HttpResult<PageResult<WebPosition>> query(PageQuery pageQuery, QueryParams params){
        return super.queryResult(pageQuery, params);
    }

    @Log(value = "导出" + SUBJECT, data = "#p0")
    @ExportMapping
    public void exports(String filename, QueryParams params, HttpServletResponse resp){
        super.exportExcel(filename, params, resp);
    }

    @Log(value = "导入" + SUBJECT)
    @ImportMapping
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @ApiOperation("获取位置内容")
    @GetMapping("contents")
    public HttpResult<List<WebContentVo>> contents(@RequestParam String code, Integer pageSize) {
        return HttpResult.data(service.contents(code, pageSize));
    }

}

